package cn.luoshaocheng.pet.controller;

import cn.luoshaocheng.basic.annotation.PreAuthorize;

import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.basic.util.jsonResult;
import cn.luoshaocheng.pet.domain.PetDetail;
import cn.luoshaocheng.pet.query.PetDetailQuery;
import cn.luoshaocheng.pet.service.IPetDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/PetDetail")
public class PetDetailController {

    @Autowired
    private IPetDetailService PetDetailService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping

    public List<PetDetail> loadAll() {
        return PetDetailService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public PetDetail loadById(@PathVariable("id") Long id) {
        return PetDetailService.loadById(id);
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    public jsonResult deleteById(@PathVariable("id") Long eid) {
        try {
            PetDetailService.delete(eid);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param PetDetail
     * @return
     */
    @PutMapping
    public jsonResult addAndUpdate(@RequestBody PetDetail PetDetail) {
        try {
            if (PetDetail.getId() == null) {
                PetDetailService.save(PetDetail);
            }else{
                PetDetailService.update(PetDetail);
            }
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param PetDetailQuery
     * @return
     */
    @PostMapping
    public PageList<PetDetail> queryPage(@RequestBody PetDetailQuery PetDetailQuery) {
        return PetDetailService.queryPage(PetDetailQuery);

    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping
    public jsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            PetDetailService.patchDel(ids);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }
}

